package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.PayReminders;
import com.ruoyi.system.domain.vo.FeeContractVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PaymentContract;
import com.ruoyi.system.service.IPaymentContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租售合同管理Controller
 * 
 * @author fcs
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/system/contract")
public class PaymentContractController extends BaseController
{
    @Autowired
    private IPaymentContractService paymentContractService;

    /**
     * 查询租售合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(PaymentContract paymentContract)
    {
        startPage();
        List<PaymentContract> list = paymentContractService.selectPaymentContractList(paymentContract);
        return getDataTable(list);
    }

    /**
     * 导出租售合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "租售合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentContract paymentContract)
    {
        List<PaymentContract> list = paymentContractService.selectPaymentContractList(paymentContract);
        ExcelUtil<PaymentContract> util = new ExcelUtil<PaymentContract>(PaymentContract.class);
        util.exportExcel(response, list, "租售合同管理数据");
    }

    /**
     * 获取租售合同管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(paymentContractService.selectPaymentContractById(id));
    }

    /**
     * 新增租售合同管理
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "租售合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaymentContract paymentContract)
    {
        return toAjax(paymentContractService.insertPaymentContract(paymentContract));
    }


    /**
     * 新增租售合同收费项
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "租售收费项管理", businessType = BusinessType.INSERT)
    @PostMapping("/addPay")
    public AjaxResult addPay(@RequestBody FeeContractVo feeContractVo)
    {
        feeContractVo.getFeeList().forEach(System.out::println);
        return toAjax(paymentContractService.insertPayReminders(feeContractVo));
    }

    /**
     * 修改租售合同管理
     */
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "租售合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaymentContract paymentContract)
    {
        return toAjax(paymentContractService.updatePaymentContract(paymentContract));
    }

    /**
     * 删除租售合同管理
     */
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "租售合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        int i = paymentContractService.deletePaymentContractById(id);
        if (i == -1)
        {
            return AjaxResult.warn("该合同有款项未结清，不能撤销");
        }
        return toAjax(i);
    }
}
